/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package neo.workforce.controller;

import java.io.IOException;
import java.io.InputStream;
import javax.servlet.ServletContext;
import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 *
 * @author krisada
 */
@Controller
public class ImageController {
    private Logger log = Logger.getLogger(this.getClass());
    @Autowired
    ServletContext context;

    public void setContext(ServletContext context) {
        this.context = context;
    }

    public ServletContext getContext() {
        return context;
    }
    

    public ImageController() {
        if( context == null )
        log.info("Servlet context is null");
        else
        log.info("Servlet context is not null");    
        
    }
    
    @RequestMapping (value="/imageedit", method = RequestMethod.GET)
    public @ResponseBody byte[] show() throws IOException {
        InputStream in = context.getResourceAsStream("/imgs/edit.png");
        
        //log.info("response image:"+ imageId);
        final HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.IMAGE_PNG);
        return IOUtils.toByteArray(in);
        /////return new ResponseEntity<byte[]>(IOUtils.toByteArray(in), headers, HttpStatus.CREATED);
    }
}
